/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vescapecloud.network;

import java.util.ArrayList;
import org.openstack4j.api.Builders;
import org.openstack4j.api.OSClient;
import org.openstack4j.model.compute.ActionResponse;
import org.openstack4j.model.compute.FloatingIP;
import org.openstack4j.model.compute.Personality;
import org.openstack4j.model.compute.Server;
import org.openstack4j.model.compute.ServerCreate;
import org.openstack4j.openstack.OSFactory;
import org.apache.commons.codec.binary.Base64;


/**
 *
 * @author JulieDu
 */
public class StandAloneTest {

    /* Configuration  for OpenStack Instance */
    // ID of Snapshot used to run an instance
    private  static final String GREEDY_IMAGE_ID = "840f1631-0cb9-4711-b618-9140e3df6114";

    // ID of the flavor used for the new instance
    private static final String GREEDY_FLAVOR_ID = "4b42c24b-bfd4-4e24-9517-36cc433e6397";

    // ID of the network installed on the new instance
    private static final String NETWORK_ID = "39366143-32ec-4dc2-a26c-fc11dcbd2592";

    // Prefix of the future instance 
    private static final String GREEDY_INSTANCE_PREFIX = "GreedyRabbit";

    // Name of external network for the instance
    private static final String EXT_NETWORK = "ext-net";

    // Authentification client
    private static  OSClient os;
    
    private static ArrayList<String> netList ; 

    public static void main(String[] args) throws InterruptedException {

        
        byte[] encodedBytes = Base64.encodeBase64("Coucou openstack !".getBytes());

        os = OSFactory.builder()
                .endpoint("http://10.0.0.11:5000/v2.0")
                .credentials("vescape", "Vescape")
                .tenantName("vescape")
                .authenticate();

        System.out.println(
                "[Instance       ]     Authentification to OpenStack ok");

        // network list
        netList = new ArrayList<String>();

        netList.add(NETWORK_ID);

       // Personality p = new Personality("/home/testPerso.txt", "coucou");
        // Create a Server Model Object
        ServerCreate sc = Builders
                .server()
                .name(GREEDY_INSTANCE_PREFIX + "_" + "ipTest")
                .flavor(GREEDY_FLAVOR_ID)
                .image(GREEDY_IMAGE_ID)
                .networks(netList)
                .addPersonality("/launchGreedy.txt", new String(encodedBytes))
                .build();

        System.out.println(
                "[Instance       ]     Server Model Object created");

        // Boot the server
        Server server = os.compute()
                .servers()
                .boot(sc);

        System.out.println(
                "[Instance       ]     Server boot");
        
         Thread.sleep(15000);
        //associate ip address of server to a floating ip adress
        os = OSFactory.builder()
                .endpoint("http://10.0.0.11:5000/v2.0")
                .credentials("vescape", "Vescape")
                .tenantName("vescape")
                .authenticate();
     //   FloatingIP ip = os.compute().floatingIps().allocateIP(EXT_NETWORK);

        ActionResponse r = os
                .compute()
                .floatingIps()
                .addFloatingIP(
                        server, server.getAccessIPv4(), "10.0.2.53");

        System.out.println("[Instance       ]     Floating IP address associated");
    }

}
